package com.totoro.utils;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * @说明 自定义异常基础类
 * @author XiongSir
 *
 */
public class BaseRuntimeException extends RuntimeException{
 
	private static final long serialVersionUID = -434104233779192938L;
	private int severity = ErrorLevel.NORMAL;
	private ErrorCodeEnum errorEnum;

	//private Throwable cause;//异常
	private String traceId;//追踪id
	
	public BaseRuntimeException(ErrorCodeEnum error,int level) {
		super( error.getMessage());
		this.setSeverity(level);
		this.setErrorEnum(error);
	}
	public BaseRuntimeException(ErrorCodeEnum error) {
		super( error.getMessage());
		this.setErrorEnum(error);
	}
	public BaseRuntimeException(ErrorCodeEnum error,Throwable cause) {
		super("->"+error.getMessage(),cause,true,true);
		this.setErrorEnum(error);
	}
	public BaseRuntimeException(ErrorCodeEnum error,Throwable cause,int level) {
		super( error.getMessage(),cause);
		this.setErrorEnum(error);
		this.setSeverity(level);
	}
	@Override
	public synchronized Throwable fillInStackTrace() {
		// TODO Auto-generated method stub
		return super.fillInStackTrace();
	}

	@Override
	public synchronized Throwable getCause() {
		// TODO Auto-generated method stub
		return super.getCause();
	}
	
	@Override
	public void printStackTrace() {
		this.printStackTrace(System.err);
	}
	
	
	@Override
	public void printStackTrace(PrintStream ps){
		if(null == getCause()){
			super.printStackTrace(ps);
		}else{
			ps.println(this);
			getCause().printStackTrace(ps);
		}
	}
	
	@Override
	public void printStackTrace(PrintWriter pw){
		if(null == getCause()){
			super.printStackTrace(pw);
		}else{
			pw.println(this);
			getCause().printStackTrace(pw);
		}
	}

	@Override
	public String getMessage(){
		if (getCause() == null) {
			return super.getMessage();
		}
		return super.getMessage() + getCause().getMessage();
	}
	public ErrorCodeEnum getErrorEnum() {
		return errorEnum;
	}
	private void setErrorEnum(ErrorCodeEnum errorEnum) {
		this.errorEnum = errorEnum;
	}
	public int getSeverity() {
		return severity;
	}
	public void setSeverity(int severity) {
		this.severity = severity;
	}

	
	
}
